package qf.wlh.com.oneprisdent;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import java.util.ArrayList;
import java.util.List;

import qf.wlh.com.oneprisdent.speakfragment.DateFragment;
import qf.wlh.com.oneprisdent.speakfragment.LikeFragment;
import qf.wlh.com.oneprisdent.speakfragment.WatchFragment;

public class SpeakActivity extends AppCompatActivity {
    private List<Fragment> list=new ArrayList<>();
    private RadioGroup radiogroup;
    private FragmentManager manager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_speak);
        initFragment();

        initView();


    }

    private void initFragment() {
        DateFragment dateFragment=new DateFragment();
        WatchFragment watchFragment=new WatchFragment();
        LikeFragment likeFragment=new LikeFragment();

        list.add(dateFragment);
        list.add(watchFragment);
        list.add(likeFragment);

    }

    private void initView() {
        radiogroup = ((RadioGroup) findViewById(R.id.speak_radioGroup));
        manager=getSupportFragmentManager();
        initListerne();
    }

    private void initListerne() {
        radiogroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {
                int count = radioGroup.getChildCount();
                boolean isChecked=false;
                View view=null;
                for (int i = 0; i < count; i++) {
                    view = radioGroup.getChildAt(i);
                    isChecked=((RadioButton)view).isChecked();
                    if (isChecked){

                        FragmentTransaction beginTransaction=manager.beginTransaction();
                        beginTransaction.replace(R.id.container,list.get(i),"tag");
                        beginTransaction.commit();
                        break;
                    }

                }



            }
        });
        //设置第一页默认选中
        ((RadioButton)radiogroup.getChildAt(0)).setChecked(true);
    }
}
